<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8"/>
    <title>用户管理 - Vue 3 组合API示例</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        ul {
            list-style: none;
            padding: 0;
        }
        li {
            padding: 8px;
            margin: 4px 0;
            border: 1px solid #ddd;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        button {
            margin-left: 10px;
        }
    </style>
</head>
<body>
<div id="app">
    <h1>用户管理</h1>
    <input v-model="newUser" placeholder="请输入用户名" />
    <br>
    <br>
    <input v-model="newUser_phone" placeholder="请输入手机号" />
    <p></p>
    <button @click="addUser">添加用户</button>

    <h2>用户列表</h2>
    <ul>
        <li>
            <span>用户名</span>
            <span>手机号</span>
            <span>操作</span>
        </li>
        <li v-for="(user, index) in users" :key="user.id">
            <span>{{ user.name }}</span>
            <span>{{ user.phone }}</span>
            <button @click="deleteUser(index)">删除</button>
        </li>
    </ul>
</div>

<script src="https://unpkg.com/vue@3"></script>
<script>
    const { createApp, ref } = Vue;

    const app = createApp({
        setup() {
            // 定义用户列表和新用户输入
            const users = ref([]);
            const newUser = ref('');
            const newUser_phone = ref('');
            // 添加用户的方法
            const addUser = () => {
                if (newUser.value.trim() !== '' && newUser_phone.value.trim()!== '') { // 确保输入不为空
                    users.value.push({
                        id: users.value.length + 1, // 简单的 ID 生成方式
                        name: newUser.value,
                        phone: newUser_phone.value
                    });
                    newUser.value = ''; // 清空输入框
                    newUser_phone.value = '';
                }

            };

            // 删除用户的方法
            const deleteUser = (index) => {
                users.value.splice(index, 1); // 根据索引删除用户
            };

            return { users, newUser,newUser_phone, addUser, deleteUser };
        }
    });

    app.mount('#app');
</script>
</body>
</html>